"""
Case Type   : SQL
Case Name   : 兼容B库中执行update
Create At   : 2023/04/14
Owner       : cr13
Description :
    1.创建兼容B库
    2.B库下建表,字段类型为布尔类型时,指定auto_increment属性
    3.清理环境
Expect      :
    1.成功
    2.合理报错
    3.成功
History     :
"""

import os
import unittest
from testcase.utils.Common import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.commsh = CommonSH('PrimaryDbUser')
        self.con = Constant()
        self.database = 'd_dml_update_0035'
        self.tb_name = 't_dml_update_0035'

    def test_ddl_table(self):
        text = "----step1:创建兼容B库 expect:成功-----"
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.database};create database " \
            f"{self.database} dbcompatibility 'B';"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.con.DROP_DATABASE_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.con.CREATE_DATABASE_SUCCESS, result, '执行失败' +
                      text)

        text = '-----step2:B库下建表,字段类型为布尔类型时,指定' \
               'auto_increment属性 expect:合理报错-----'
        self.log.info(text)
        result = self.commsh.execut_db_sql(
            f"drop table if exists {self.tb_name};create table "
            f"{self.tb_name}(col1 boolean primary key auto_increment);",
            dbname=self.database)
        self.log.info(result)
        self.assertIn(self.con.TABLE_DROP_SUCCESS, result, '执行失败' + text)
        self.assertIn("ERROR:  The datatype of column 'col1' does not "
                      "support auto_increment", result, '执行失败' + text)

    def tearDown(self):
        text = '-----step3:清理环境 expect:成功-----'
        self.log.info(text)
        result = self.commsh.execut_db_sql(
            f"drop database if exists {self.database};")
        self.log.info(result)
        self.assertIn(self.con.DROP_DATABASE_SUCCESS, result, '执行失败' +
                      text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
